home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / c / SUPRALib.lha / SUPRALib / Developer / Source / FCopy.c < prev    next >
C/C++ Source or Header  |  1999-05-17  |  4KB  |  173 lines

  1. /****** FCopy **************************************************************
  2. *
  3. *   NAME
  4. *       FCopy -- copies source file to destination file (V10)
  5. *       (dos V36)
  6. *
  7. *       Please use FCopyTags() instead.
  8. *
  9. *   SYNOPSIS
  10. *       error = FCopy(source, dest, buffer)
  11. *
  12. *       UBYTE = FCopy(char *, char *, LONG);
  13. *
  14. *   FUNCTION
  15. *       This function works very similar to C:Copy program. It copies
  16. *       a source file to a destination file.
  17. *       Please see more powerful FCopyTags() function (asynchronous).
  18. *
  19. *   INPUTS
  20. *       source - pointer to a source file name (with a relative or
  21. *                absolute path)
  22. *       dest - pointer to a destination file name
  23. *       buffer - maximum size of a buffer (in bytes) to be
  24. *                allocated for copying. If this buffer is 0, FCopy()
  25. *                will try to allocate buffer a size of a source file,
  26. *                or the largest memory block available. (this is the
  27. *                fastest way).
  28. *
  29. *   RESULT
  30. *       error - zero if no error. Function may return one of the
  31. *       following error definitions:
  32. *
  33. *           FC_ERR_EXIST - Source file does not exist
  34. *           FC_ERR_EXAM  - Error during examination of a source file
  35. *           FC_ERR_MEM   - Not enough memory availabe
  36. *           FC_ERR_OPEN  - Source file could not be oppened
  37. *           FC_ERR_READ  - Error while reading a source file
  38. *           FC_ERR_DIR   - Source file path is a directory
  39. *           FC_ERR_DEST  - Destination file could not be created
  40. *           FC_ERR_WRITE - Error while writing to a destination file
  41. *
  42. *   EXAMPLE
  43. *
  44. *       \* This example will copy a file c:dir to ram: with a new name
  45. *        * list.
  46. *        *\
  47. *
  48. *       UBYTE err;
  49. *
  50. *       if ((err = FCopy("C:Dir", "ram:list", 0)) == 0) {
  51. *
  52. *           no errors...
  53. *
  54. *       } else {
  55. *           printf("Error: %d\n", err); \* Error occured during FCopy() *\
  56. *
  57. *       }
  58. *
  59. *   NOTES
  60. *       If an error occurs then a destination file will not be deleted
  61. *       if it has already been partly copied.
  62. *
  63. *   CHANGES
  64. *        (December'98)
  65. *        See the example in this text and look to the line with FCopy.
  66. *       Don't work correctly when compiling with vbcc. I don't know
  67. *       the reason of the problem but I know the way to correct it.
  68. *       Expanding the link like "err = FCopy(...);" and second line
  69. *       "if (!err) {" work correct. So I change a lot of code.
  70. *       Second, I change the file-I/O, because there was another
  71. *       problem with the EOF so the original function don't work.
  72. *        Testfunction now work fine.
  73. *       Greeting from Berlin, Germany.         cu, Michaela Prüß
  74. *
  75. ************************************************************************/
  76.  
  77. #include <exec/memory.h>
  78. #include <proto/dos.h>
  79. #include <proto/exec.h>
  80. #include <libraries/supra.h>
  81.  
  82.  
  83. UBYTE FCopy(char *source, char *dest, LONG buf)
  84. {
  85.     struct    FileInfoBlock    fib;
  86.  
  87.     LONG    fsize;
  88.     LONG    max;  /* part = One part of buffer */
  89.     APTR    mem=NULL;
  90.     LONG    len=0;
  91.     BPTR    lock;
  92.     BPTR    fsource=NULL;
  93.     BPTR    fdest=NULL;
  94.     UBYTE    err=0;
  95.  
  96.     lock = Lock(source, ACCESS_READ);
  97.     if (!lock) return(FC_ERR_EXIST);
  98.  
  99.     if (!Examine(lock, &fib))
  100.     {
  101.         UnLock(lock);
  102.         return(FC_ERR_EXAM);
  103.     }
  104.  
  105.     if (fib.fib_DirEntryType < 0)
  106.     {
  107.         fsize = fib.fib_Size;
  108.  
  109.         fdest = Open(dest, MODE_NEWFILE);
  110.         if (fdest)
  111.         {
  112.             if (!buf) max = AvailMem(MEMF_LARGEST);
  113.             else max = buf;
  114.  
  115.             if (max > fsize) max = fsize;
  116.  
  117.             while (max>1024 && !mem)
  118.             {
  119.                 mem = AllocMem(max, 0L);
  120.                 if (!mem) max-=1024;
  121.             }
  122.  
  123.             if (mem)
  124.             {
  125.                 fsource = OpenFromLock(lock);
  126.                 if (fsource)
  127.                 {
  128.                     do
  129.                     {
  130.                         len=Read(fsource, mem, max);
  131.                         if (len<0)
  132.                         {
  133.                             err = FC_ERR_READ;
  134.                              break;
  135.                         }
  136.                         if (!len) break;
  137.  
  138.                         if (!Write(fdest, mem, len))
  139.                         {
  140.                             err = FC_ERR_WRITE;
  141.                             break;
  142.                         }
  143.                     }
  144.                     while(TRUE);
  145.                 }
  146.                 else
  147.                 {
  148.                     err = FC_ERR_OPEN; /* if OpenFromLock */
  149.                 }
  150.             }
  151.             else
  152.             {
  153.                 err = FC_ERR_MEM; /* Not enough memory */
  154.             }
  155.         }
  156.         else
  157.         {
  158.             err = FC_ERR_DEST;   /* if Destination File Open */
  159.         }
  160.     }
  161.     else
  162.     {
  163.          err = FC_ERR_DIR;    /* If source is dir */
  164.     }
  165.  
  166.     if (fsource) Close(fsource);
  167.     if (fdest) Close(fdest);
  168.     if (lock) UnLock(lock);
  169.     if (mem) FreeMem(mem, max);
  170.  
  171.     return(err);
  172. }
  173.